package com.ksyun.campus.metaserver.web;

import com.ksyun.campus.metaserver.common.result.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 全局异常托底类
     *
     * @param request   http servlet请求对象
     * @param exception 捕获的异常
     */
    @ExceptionHandler(value = Throwable.class)
    public BaseResult defaultException(
            HttpServletRequest request,
            Throwable exception
    ) {
        log.error(" requestUrl{} [msg] {}",
                request.getRequestURL().toString(),
                exception.getMessage()
        );
        return BaseResult.error(String.format("请求无法正确处理 [msg]: %s", exception.getMessage()));
    }
}
